package com.clz.hyperit.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.clz.hyperit.entity.Position;
import com.clz.hyperit.service.PositionService;
import com.clz.hyperit.util.ResultMessage;

@RestController
@RequestMapping(value = "/position")
public class PositionController {
	private ResultMessage result = null;
	private final Logger logger = LoggerFactory.getLogger(PositionController.class);
	@Autowired
	private PositionService positionService;
	
	@RequestMapping(value = "/savePosition", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> savePosition(Position position){
		result = new ResultMessage();
		if(positionService.getByName(position.getName())!=null){
			result.setResultInfo("已存在该职位");
			result.setServiceResult(false);
		}
		else{
			positionService.savePosition(position);
			result.setResultInfo("创建职业成功");
			result.setServiceResult(true);
		}
		Position position_vo = positionService.getByName(position.getName());
		result.getResultParm().put("position",position_vo);
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/deletePosition", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> deletePosition(Position position){
		result = new ResultMessage();
		positionService.deletePosition(position);
		result.setResultInfo("删除职业成功");
		result.setServiceResult(true);
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	@RequestMapping(value = "/updatePosition", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> updatePosition(Position position){
		result = new ResultMessage();
		positionService.updatePosition(position);
		result.setResultInfo("更新职业成功");
		result.setServiceResult(true);
		result.getResultParm().put("position_id",position.getId());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	@RequestMapping(value = "/findPosition", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findPosition(){
		result = new ResultMessage();
		List<Position> positionlist = positionService.findPosition();
		result.setResultInfo("职位获取成功");
		result.setServiceResult(true);
		result.getResultParm().put("positionlist",positionlist);
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
}
